<template>
	<div class="login-wrap">
		<div class="ms-login" v-show="register_visiable">
			<div class="ms-title">博客登录</div>
			<el-form :model="ruleForm" :rules="login_rules" ref="ruleForm" label-width="0px" class="ms-content">
				<el-form-item prop="user_code">
					<el-input v-model="ruleForm.user_code" placeholder="请输入用户名">
						<el-button slot="prepend" icon="el-icon-lx-people"></el-button>
					</el-input>
				</el-form-item>
				<el-form-item prop="password">
					<el-input type="password" placeholder="请输入密码" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')">
						<el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
					</el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
				</div>
				<div class="login-btn">
					<el-button type="primary" @click="open_login(false)">注册</el-button>
				</div>
			</el-form>
		</div>

		<div class="ms-register" v-show="!register_visiable">
			<div class="ms-title">注册</div>
			<el-form :model="registerForm" :rules="register_rules" ref="registerForm" class="ms-content">
				<el-form-item label="用户名:" prop="user_code">
					<el-input v-model="registerForm.user_code" placeholder="请输入用户名"></el-input>
				</el-form-item>
				<el-form-item label="昵称:" prop="user_name">
					<el-input v-model="registerForm.user_name" placeholder="请输入昵称"></el-input>
				</el-form-item>
				<el-form-item label="密码:" prop="password">
					<el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
				</el-form-item>
				<el-form-item label="手机号:" prop="mobile">
					<el-input type="phone" v-model="registerForm.mobile" placeholder="请输入手机号"></el-input>
				</el-form-item>
				<div class="login-btn">
					<el-button type="primary" @click="open_register('registerForm')">注册</el-button>
				</div>
				<div class="login-btn">
					<el-button type="primary" @click="open_login(true)">返回</el-button>
				</div>
			</el-form>
		</div>
	</div>
</template>

<script>
	import {
		constHelp
	} from '../common/contant';
	import common from '../common/common.js'
	import bus from '../common/bus';
	export default {
		data: function() {
			// 设置手机号的验证规则
			const checkPhone = (rule, value, callback) => {
				if (!value) {
					callback(new Error('请输入联系方式'))
				} else {
					const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
					if (reg.test(value)) {
						callback()
					} else {
						return callback(new Error('请输入正确的电话'))
					}
				}
			}
			return {
				register_visiable: true,
				registerForm: {},
				url: {
					login: constHelp['WEBAPIURL'] + '/user/login',
					register: constHelp['WEBAPIURL'] + '/user/register',
				},
				ruleForm: {
					user_code: '',
					password: ''
				},
				login_rules: {
					user_code: [{
						required: true,
						message: '请输入用户名',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: 'blur'
					}]
				},
				register_rules: {
					user_code: [{
						required: true,
						message: '请输入用户名！',
						trigger: 'blur'
					}],
					user_name: [{
						required: true,
						message: '请输入昵称！',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '请输入密码！',
						trigger: 'blur'
					}],
					mobile: [{
						required: true,
						validator: checkPhone,
						message: '请输入手机号！',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.$axios.post(this.url.login, this.ruleForm).then((res) => {
							if (res.data.err == "0") {
								localStorage.setItem("UserInfo", JSON.stringify(res.data.data));
								this.$message.success("登录成功");
								this.$router.push('/');
							} else {
								this.$message.error(res.data.errMsg);
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			open_register(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.registerForm.user_type="3"
						this.$axios.post(this.url.register, this.registerForm).then((res) => {
							if (res.data.err == "0") {
								this.$message.success("注册成功");
								this.open_login(true)
							} else {
								this.$message.error(res.data.errMsg);
							}
						})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			open_login(flage) {
				this.register_visiable = flage
			}
		}
	}
</script>

<style scoped>
	.login-wrap {
		position: relative;
		width: 100%;
		height: 100%;
		background-image: url(../../assets/img/backgroundimage/login-bg.jpg);
		background-size: 100%;
	}

	.ms-title {
		width: 100%;
		line-height: 50px;
		text-align: center;
		font-size: 20px;
		color: #000;
		border-bottom: 1px solid #ddd;
	}

	.ms-login {
		position: absolute;
		left: 50%;
		top: 50%;
		width: 350px;
		margin: -190px 0 0 -175px;
		border-radius: 5px;
		background: rgba(255, 255, 255, 0.3);
		overflow: hidden;
	}

	.ms-register {
		position: absolute;
		left: 50%;
		top: 40%;
		width: 350px;
		margin: -190px 0 0 -175px;
		border-radius: 5px;
		background: rgba(255, 255, 255, 0.3);
		overflow: hidden;
	}

	.ms-content {
		padding: 30px 30px;
	}

	.login-btn {
		text-align: center;
	}

	.login-btn button {
		width: 100%;
		height: 36px;
		margin-bottom: 10px;
	}

	.login-tips {
		font-size: 12px;
		line-height: 30px;
		color: #fff;
	}
</style>
